package org.lee.common.domain;

import lombok.Data;
import lombok.Getter;

import java.io.Serializable;

@Data
public class Result implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer code;

    private String message;

    private Result() {
    }

    public Result(ResultCode resultCode, String message) {
        this.code = resultCode.code();
        this.message = message;
    }

    private void setResultCode(ResultCode resultCode) {
        this.code = resultCode.code();
        this.message = resultCode.message();
    }

    // 返回成功
    public static Result success() {
        Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    // 返回成功
    public static <T> Result success(T data) {
        ResultInner<T> result = new ResultInner<T>(ResultCode.SUCCESS, ResultCode.SUCCESS.message(), data);
        return result;
    }

    // 返回失败
    public static Result fail(Integer code, String message) {
        Result result = new Result();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    // 返回失败
    public static Result fail(ResultCode resultCode, String message) {
        return fail(resultCode.code(), message);
    }

    // 返回失败
    public static Result fail(ResultCode resultCode) {
        Result result = new Result();
        result.setResultCode(resultCode);
        return result;
    }

    @Getter
    public static class ResultInner<T> extends Result {
        private T data;

        public ResultInner(ResultCode code, String message, T data) {
            super(code, message);
            this.data = data;
        }
    }

}

